<?php
namespace App\HttpController;

use EasySwoole\Core\Http\AbstractInterface\Controller;
use EasySwoole\Core\Http\Request;
use EasySwoole\Core\Http\Response;
use \EasySwoole\Core\Component\Logger;

use EasySwoole\Core\Component\Pool\PoolManager;
use App\HttpController\CommonController;
use App\Common\Common;


/**
 * Class Register
 * @package App\HttpController
 */
class Operational extends Controller
{
    function index()
    {
        $this->response()->write('Operational-testing easySwoole!');
    }

    /**
     * [save 保存测试接口参数]
     * @DateTime 2018-09-11T20:41:41+0800
     * @刘家腾
     * @return   [type]                   [description]
     */
    public function save()
    {
        $post=$this->request()->getRequestParam();
        $pool = PoolManager::getInstance()->getPool('App\Utility\MysqlPool2'); // 获取连接池对象
        $pool_db = $pool->getObj($timeOut = 0.1);
        $creationtime = date("Y-m-d H:i:s");
        if( !empty($post['getpost']) ){//提交方式
            $data['getpost']=$post['getpost'];
        }
        if( !empty($post['interface_name']) ){//接口名
            $data['interface_name']=$post['interface_name'];
        }
        if( !empty($post['url']) ){//URL
            $data['url']=$post['url'];
        }
        if( !empty($post['introduction']) ){//接口描述
            $data['introduction']=$post['introduction'];
        }
        $user_id=1;
        $system_id=1;
        $data['system_id']=$user_id;
        $data['user_id']=$system_id;
        $data['creationtime']=$creationtime;

        $pool_db->where('system_id',$system_id);
        $pool_db->where('user_id',$user_id);
        $pool_db->where('interface_name',$data['interface_name']);
        $info = $pool_db->get('operational');
        if( !empty($info) ){
            $pool->getObj($pool_db);
           return $this->writeJson('1001',$info,'[ '.$data['interface_name'].' ]  已经存在！');

        }

        $operational_id = $pool_db->insert('operational', $data);

        foreach ($post['data'] as $key => $value) {
            $data_list['system_id']=$system_id;
            $data_list['user_id']=$user_id;
            $data_list['operational_id']=$operational_id;
            $data_list['name']=$value['0'];
            $data_list['value']=$value['1'];
            $data_list['type']=$value['2'];
            $data_list['isnull']=$value['3'];
            $data_list['blurb']=$value['4'];
            $data_list['creationtime']=$creationtime;
            $id = $pool_db->insert('operational_testing', $data_list);

            $id_list= $id_list.'~~'.$id;
        }

        $pool->getObj($pool_db);
        return $this->writeJson('200',$data_list,$data);

    }

    public function get_list()
    {
        $post=$this->request()->getRequestParam();
        $pool = PoolManager::getInstance()->getPool('App\Utility\MysqlPool2'); // 获取连接池对象
        $pool_db = $pool->getObj($timeOut = 0.1);
        $creationtime = date("Y-m-d H:i:s");
        if( !empty($post['getpost']) ){//提交方式
            $data['getpost']=$post['getpost'];
        }
        

        $pool->getObj($pool_db);
        return $this->writeJson('200',$data_list,$data);
    }
    protected function onRequest($action): ?bool
    {
    	$this->request()->withAttribute('requestTime', microtime(true));
        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }
    protected function actionNotFound($action):void
    {
        $this->response()->write('Register actionNotFound');
    }
    protected function afterAction($actionName):void
    {
    	
    }
}
